﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI.SystemManage
{
    public partial class ParameterEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SelectPanel();
                Bind();
                bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
                bnClose2.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();

            }
        }

        private void Bind()
        {
            string action = Request.QueryString["action"].ToString();
            if (action == "new" || action == "")
            {
                dl_parameterIndex.DataSource = BLL.Parameter.ListIndex();
                dl_parameterIndex.DataTextField = "value";
                dl_parameterIndex.DataValueField = "id";
                dl_parameterIndex.DataBind();
                ExtAspNet.ListItem item = new ExtAspNet.ListItem("请选择", "-1");
                dl_parameterIndex.Items.Insert(0,item);
            }
            if (action == "edit")
            {
                int id = int.Parse(Request.QueryString["id"].ToString());
                Model.Parameter m;
                m = BLL.Parameter.SelectOne(id);
                tb_editparameter.Text = m.value;
                dl_editparameterIndex.DataSource = BLL.Parameter.ListIndex();
                dl_editparameterIndex.DataTextField = "value";
                dl_editparameterIndex.DataValueField = "id";
                dl_editparameterIndex.DataBind();
                dl_editparameterIndex.SelectedValue = m.indexID.ToString();
                
            }
            if (action == "del")
            {
                    int id = int.Parse(Request.QueryString["id"].ToString());
                    if (BLL.Parameter.Del(id))
                    {
                        ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());

                    }
                    else
                    {
                        ExtAspNet.Alert.Show("删除失败！", "失败", ExtAspNet.MessageBoxIcon.Error);

                    }
             }
        }

        private void SelectPanel()
        {
            string action = Request.QueryString["action"].ToString();
            switch (action) 
            { 
                case "new":
                    AddPanel.Hidden = false;
                    EditPanel.Hidden = true;
                    break;
                case "edit":
                    AddPanel.Hidden = true;
                    EditPanel.Hidden = false;
                    break;
                default :
                    AddPanel.Hidden = false;
                    EditPanel.Hidden = true;
                    break;
            }
        }

        protected void bnSave_Click(object sender, EventArgs e)
        {
                Model.Parameter m = new Model.Parameter();
                m.value = tb_parameter.Text;
                m.indexID = int.Parse(dl_parameterIndex.SelectedValue);
                if (BLL.Parameter.Add(m))
                {
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
                }
                else
                {
                    ExtAspNet.Alert.Show("添加失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
                }  
        }

        protected void bnEdit_Click(object sender, EventArgs e)
        {
            Model.Parameter m = new Model.Parameter();
            m.id = int.Parse(Request.QueryString["id"].ToString());
            m.value = tb_editparameter.Text;
            m.indexID = int.Parse(dl_editparameterIndex.SelectedValue);
            if (BLL.Parameter.UpdateOne(m))
            {
                ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
            }
            else
            {
                ExtAspNet.Alert.Show("编辑失败！", "失败", ExtAspNet.MessageBoxIcon.Error);
            }
        }
    }
}